package com.bsj.pay.util.security;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AES {
	public static final String EncryptMode = "ECB";

	public static String AES_Encrypt(Object plaintext, String Key) {
		String PlainText = null;
		try {
			PlainText = plaintext.toString();
			if (Key == null) {
				return null;
			}
			byte[] raw = Key.getBytes("utf-8");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(1, skeySpec);
			byte[] encrypted = cipher.doFinal(PlainText.getBytes("utf-8"));
			return Base64.encode(encrypted);
		} catch (Exception ex) {
			System.out.println(ex.toString());
		}
		return null;
	}

	public static String AES_Decrypt(Object cipertext, String Key) {
		String CipherText = null;
		try {
			CipherText = cipertext.toString();
			if (Key == null) {
				return null;
			}
			byte[] raw = Key.getBytes("utf-8");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

			cipher.init(2, skeySpec);

			byte[] encrypted1 = Base64.decode(CipherText);
			try {
				byte[] original = cipher.doFinal(encrypted1);
				return new String(original, "utf-8");
			} catch (Exception e) {
				System.out.println(e.toString());
				return null;
			}
		} catch (Exception ex) {
			System.out.println(ex.toString());
		}
		return null;
	}
	public static void main(String[] args) {
		
		System.out.println(AES.AES_Decrypt("WRIF46imD9BBrM8IteAhYw==", "JSzaJtDF4CW5XnZr"));
	}
}
